package address;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.border.LineBorder;

/*
 * 分组选择界面
 */
/**
 *
 * @author Mavis
 */
public class SelectBelongs extends JFrame {

    ArrayList Belongs = new ArrayList();   //用于存放每个JCheckBox组件       
    ArrayList belongs = new ArrayList();
    People people = null;
    Font f = new Font("微软雅黑", Font.PLAIN, 18);
    Font f2 = new Font("微软雅黑", Font.PLAIN, 12);

    SelectBelongs(ArrayList belongs, People people) {  //belongs 记录通讯录中原有的分组            

        this.belongs = belongs;
        this.people = people;
        SelectPanel selectP = new SelectPanel();
        selectP.setBackground(Color.white);
        add(selectP);
        selectP.setFocusable(true);
        Belongs.clear();
    }

    private class SelectPanel extends JPanel {

        JPanel selectpanel = new JPanel();
        ArrayList Belongs = new ArrayList();
        JCheckBox jchb;

        public SelectPanel() {  //存放着已有分组
            setLayout(null);
            selectpanel.setBackground(Color.white);
            selectpanel.setLayout(null);
            selectpanel.setBounds(0, 0, 400, 800);
            JScrollPane jscp = new JScrollPane();
            jscp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
            selectpanel.setPreferredSize(new Dimension(350, 300));
            jscp.setViewportView(selectpanel);
            jscp.setBounds(0, 0, 400, 280);
            add(jscp);
            
            ImageIcon Icon1 = new ImageIcon("img/newGroup.png");    //增加分组jbt
            JButton jbtAdd = new JButton(Icon1);
            jbtAdd.setBorderPainted(false);
            jbtAdd.setBounds(15, 325, 120, 30);
  
            jbtAdd.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent ae) {
                    AddBelongs frame = new AddBelongs();
                    frame.setSize(300, 200);
                    frame.setTitle("新建分组");
                    frame.setLocationRelativeTo(null);
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setVisible(true);
                }

            });
            add(jbtAdd);
            ImageIcon Icon2 = new ImageIcon("img/confirm.png");    //增加分组jbt
            JButton jbtOK = new JButton(Icon2);
            jbtOK.setBorderPainted(false);
            jbtOK.setBounds(160, 325, 100, 30);      
            jbtOK.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent ae) {
                    int i;
                    JCheckBox j = null;
                    String s;
                    people.belongs.clear();
                    for (i = 0; i < Belongs.size(); i++) {
                        j = (JCheckBox) Belongs.get(i);
                        if (j.isSelected()) {
                            s = j.getActionCommand();
                            people.belongs.add(s);
                        }

                    }

                    System.out.println(people.belongs.size());

                    dispose();
                }

            });
            add(jbtOK);
            ImageIcon Icon3 = new ImageIcon("img/cancel_newPeople.png");    //增加分组jbt
            JButton jbtreturn = new JButton(Icon3);
            jbtreturn.setBorderPainted(false);
            jbtreturn.setBounds(285, 325, 100, 30);
//            jbtreturn.setFont(f2);
            jbtreturn.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent ae) {
                    dispose();

                }

            });
            add(jbtreturn);

        }

        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            int h = 20;
            String s;
            repaint();
            //Belongs.clear();
            for (int i = 0; i < belongs.size(); i++) {
                s = (String) belongs.get(i);
                jchb = new JCheckBox(s);
                jchb.setFont(f);
                jchb.setBackground(Color.white);
                jchb.setBounds(20, h, 300, 20);
                h += 40;
//                jchb.setBorder(new LineBorder(Color.black, 1));
                selectpanel.add(jchb);
                Belongs.add(jchb);
       
 
 
            }
        }
    }

    class AddBelongs extends JFrame {

        JLabel la = new JLabel("请输入新的分组：");
        JLabel warn = new JLabel("请输入有效分组！");
        JTextField te = new JTextField();
        Font f = new Font("微软雅黑", Font.PLAIN, 18);
        Font f2 = new Font("微软雅黑", Font.PLAIN, 14);
        JButton jOK = new JButton(" 确定 ");
        JButton jcancel = new JButton(" 取消 ");
        String newbelongs = new String();
        String ss = null;

        public AddBelongs() {
            setLayout(null);
            la.setBounds(20, 20, 250, 30);

            la.setFont(f);
            warn.setBounds(20, 50, 250, 20);
            warn.setFont(f2);
            te.setBounds(20, 70, 250, 30);
            te.setText(null);
            jOK.setBounds(30, 110, 100, 30);
            jOK.setFont(f);
            jOK.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent ae) {
                    ss = te.getText();
                    if (ss.length() == 0) {
                        add(warn);
                        repaint();

                    } else {
                        belongs.add(ss);
                        dispose();
                    }

                }

            });

            jcancel.setBounds(160, 110, 100, 30);
            jcancel.setFont(f);
            jcancel.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent ae) {
                    dispose();
                }

            });

            add(la);
            add(te);
            add(jOK);
            add(jcancel);
            // add(p);
        }

        String getNewBelongs() {
            return newbelongs;
        }
    }

}
